/**
 * 
 */
package com.nari.heatshare.action;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.struts2.json.JSONUtil;
import org.apache.struts2.json.annotations.JSON;

import com.nari.baseinfo.model.IBuilding;
import com.nari.baseinfo.model.ICommunity;
import com.nari.baseinfo.model.IConcentrator;
import com.nari.baseinfo.model.IProject;
import com.nari.baseinfo.service.IBuildingService;
import com.nari.baseinfo.service.ICommunityService;
import com.nari.baseinfo.service.IConcentratorService;
import com.nari.baseinfo.service.IProjectService;
import com.nari.coreframe.action.struts2.JSONActionSupport;
import com.nari.heatshare.model.ITempPointDataStatistics;
import com.nari.heatshare.model.ITemperaturePointDataGather;
import com.nari.heatshare.service.ITempPointDataStatisticsService;
import com.nari.heatshare.service.ITemperaturePointDataGatherService;
import com.nari.heatshare.service.ITemperaturePointService;
import com.nari.heatshare.utils.queryvo.TempPointDataStatisExtendQuery;
import com.nari.platform.date.CalendarConstants;
import com.nari.platform.date.CalendarUtils;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResultWithPageCond;
import com.opensymphony.xwork2.Action;

/**
 * 
 * @author Xiaopeng
 * @date 2014-3-17
 */
public class TempPointDataStatisticsAction extends JSONActionSupport {
	private static final long serialVersionUID = 7476464926235419959L;
	private ITempPointDataStatisticsService iTempPointDataStatisticsService;
	private ITemperaturePointDataGatherService iTemperaturePointDataGatherService;
	private ITemperaturePointService iTemperaturePointService;
	private IProjectService iProjectService;
	private ICommunityService iCommunityService;
	private IBuildingService iBuildingService;
	private IConcentratorService iConcentratorService;
	//温度点数据列表查询
	private String concentratorNo;
	private Date recordTime; 
	//分页查询
	private int pageIndex = 1; //默认查询第一页列表
	private PageCond pageCond;
	private int pageSize;
	private ResultWithPageCond<ITempPointDataStatistics> tempPointDataStatisPage;
	//温度点数据统计的档案扩展信息
	private Map<String, TempPointDataStatisExtendQuery> tpdStatisExtendMap;
	private ITempPointDataStatistics tempPointDataStatis;
	private List<ITemperaturePointDataGather> tempPointDataList;
	private Map<String, String> referHouseNoMap;
	//温度点数据批次
	private String tpDataStatisUID;
	private IConcentrator concentrator;
	private Map<String, String> pageCityMap;
	private IProject project;
	private ICommunity community;
	private IBuilding building;
	private String batch2BaseinfoMatch;
	
	/**
	 * 温度点数据信息管理
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-3-22
	 */
	public String queryTempPointDataMgrNav() throws Exception {
		//根据反馈的集中器信息加载对应的所属信息
		return "tempPointDataMgr";
	}
	
	/**
	 * 分页查询温度点数据统计信息
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-4-8
	 */
	public String queryTempPointDataPage() throws Exception {
		//分页信息
		this.pageCond = new PageCond();
		int start = (this.pageIndex - 1) * this.pageCond.getLimit();
		this.pageCond.setStart(start);
		//抄表时间
		String recordTimeStr = null;
		if(this.recordTime != null) {
			recordTimeStr = CalendarUtils.parseDateToString(recordTime, CalendarConstants.dateFormat);
		}
		//查询温度点数据统计信息
		this.tempPointDataStatisPage = iTempPointDataStatisticsService.queryStatisPage(concentratorNo, recordTimeStr, pageCond);
		//计算结果集的页数
		this.pageSize = new BigDecimal(this.tempPointDataStatisPage.getPageCond().getTotal())
				.divide(new BigDecimal(this.tempPointDataStatisPage.getPageCond().getLimit()), RoundingMode.UP).intValue();
		if(pageSize == 0) pageSize = 1;
		//温度点数据统计的档案扩展信息
		this.tpdStatisExtendMap = iTempPointDataStatisticsService.queryTpdStatisExtend(tempPointDataStatisPage.getList());
		return "tpDataStatisPage";
	}
	
	/**
	 * 查询温度点数据批次详细资料
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-4-9
	 */
	public String queryTempPointDataBatchDetail() throws Exception {
		//查询温度点数据批次统计信息
		this.tempPointDataStatis = iTempPointDataStatisticsService.queryEntityById(tpDataStatisUID);
		if(tempPointDataStatis != null) {
			this.recordTime = CalendarUtils.parseStringToDate(tempPointDataStatis.getRecordTime(), CalendarConstants.dateFormat);
			//批次数据与档案信息匹配校验
			List<String> matchResultList = iTemperaturePointDataGatherService.queryLackBaseinfoSeriousNum(tempPointDataStatis.getConcentratorNo(), recordTime, null);
			if(matchResultList!=null && matchResultList.size()>0) {
				StringBuffer matchResult = new StringBuffer("");
				for(int index=0; index<matchResultList.size(); index++) {
					matchResult.append(matchResultList.get(index));
					if(index < matchResultList.size()-1) {
						matchResult.append(",");
					}
					if((index+1)%3 == 0) {
						matchResult.append("\n");
					}
				}
				this.batch2BaseinfoMatch = matchResult.toString();
			}
			//查询温度点数据批次对应的集中器信息
			this.concentrator = iConcentratorService.queryByConcentratorNo(tempPointDataStatis.getConcentratorNo());
			//根据集中器信息逆向查找对应的档案信息
			if(concentrator != null) {
				//所属项目
				this.project = iProjectService.queryEntityById(concentrator.getProjectID());
				//所属小区
				this.community = iCommunityService.queryEntityById(concentrator.getCommunityID());
				//所属楼栋
				if(concentrator.getBuildingID()!=null && !"".equals(concentrator.getBuildingID())) {
					this.building = iBuildingService.queryEntityById(concentrator.getBuildingID());
				}
			}
		}
		return "tpDataStatisDetail";
	}
	
	/**
	 * 批量删除温度点数据批次
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-4-9
	 */
	public String deleteTempPointDataBatch() throws Exception {
		this.rmsg = iTempPointDataStatisticsService.deleteTempPointDataBatch(tpDataStatisUID);
		this.writeJsonStrToResponse(JSONUtil.serialize(rmsg));
		return Action.SUCCESS;
	}
	
	/**
	 * 查询温度点数据列表
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-3-22
	 */
	public String queryTempPointDataBatch() throws Exception {
		//查询温度点数据批次列表
		this.tempPointDataList = iTemperaturePointDataGatherService.queryUploadBatchTempPointData(concentratorNo, recordTime);
		//查询温度点数据及其统计信息
		if(tempPointDataList!=null && tempPointDataList.size()>0) {
			//查询温度点数据对应的温度点编号、房间号映射表
			this.referHouseNoMap = iTemperaturePointDataGatherService.queryReferHouseNoMap(tempPointDataList, concentratorNo);
		}
		return "tpDataList";
	}

	/**
	 * @return the iTempPointDataStatisticsService
	 */
	@JSON(serialize=false)
	public ITempPointDataStatisticsService getiTempPointDataStatisticsService() {
		return iTempPointDataStatisticsService;
	}

	/**
	 * @param iTempPointDataStatisticsService the iTempPointDataStatisticsService to set
	 */
	public void setiTempPointDataStatisticsService(
			ITempPointDataStatisticsService iTempPointDataStatisticsService) {
		this.iTempPointDataStatisticsService = iTempPointDataStatisticsService;
	}

	/**
	 * @return the iTemperaturePointDataGatherService
	 */
	@JSON(serialize=false)
	public ITemperaturePointDataGatherService getiTemperaturePointDataGatherService() {
		return iTemperaturePointDataGatherService;
	}

	/**
	 * @param iTemperaturePointDataGatherService the iTemperaturePointDataGatherService to set
	 */
	public void setiTemperaturePointDataGatherService(
			ITemperaturePointDataGatherService iTemperaturePointDataGatherService) {
		this.iTemperaturePointDataGatherService = iTemperaturePointDataGatherService;
	}

	/**
	 * @return the iTemperaturePointService
	 */
	@JSON(serialize=false)
	public ITemperaturePointService getiTemperaturePointService() {
		return iTemperaturePointService;
	}

	/**
	 * @param iTemperaturePointService the iTemperaturePointService to set
	 */
	public void setiTemperaturePointService(
			ITemperaturePointService iTemperaturePointService) {
		this.iTemperaturePointService = iTemperaturePointService;
	}

	/**
	 * @return the iProjectService
	 */
	@JSON(serialize=false)
	public IProjectService getiProjectService() {
		return iProjectService;
	}

	/**
	 * @param iProjectService the iProjectService to set
	 */
	public void setiProjectService(IProjectService iProjectService) {
		this.iProjectService = iProjectService;
	}

	/**
	 * @return the iCommunityService
	 */
	@JSON(serialize=false)
	public ICommunityService getiCommunityService() {
		return iCommunityService;
	}

	/**
	 * @param iCommunityService the iCommunityService to set
	 */
	public void setiCommunityService(ICommunityService iCommunityService) {
		this.iCommunityService = iCommunityService;
	}

	/**
	 * @return the iBuildingService
	 */
	@JSON(serialize=false)
	public IBuildingService getiBuildingService() {
		return iBuildingService;
	}

	/**
	 * @param iBuildingService the iBuildingService to set
	 */
	public void setiBuildingService(IBuildingService iBuildingService) {
		this.iBuildingService = iBuildingService;
	}

	/**
	 * @return the iConcentratorService
	 */
	@JSON(serialize=false)
	public IConcentratorService getiConcentratorService() {
		return iConcentratorService;
	}

	/**
	 * @param iConcentratorService the iConcentratorService to set
	 */
	public void setiConcentratorService(IConcentratorService iConcentratorService) {
		this.iConcentratorService = iConcentratorService;
	}

	/**
	 * @return the concentratorNo
	 */
	public String getConcentratorNo() {
		return concentratorNo;
	}

	/**
	 * @param concentratorNo the concentratorNo to set
	 */
	public void setConcentratorNo(String concentratorNo) {
		this.concentratorNo = concentratorNo;
	}

	/**
	 * @return the recordTime
	 */
	public Date getRecordTime() {
		return recordTime;
	}

	/**
	 * @param recordTime the recordTime to set
	 */
	public void setRecordTime(Date recordTime) {
		this.recordTime = recordTime;
	}

	/**
	 * @return the pageIndex
	 */
	public int getPageIndex() {
		return pageIndex;
	}

	/**
	 * @param pageIndex the pageIndex to set
	 */
	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	/**
	 * @return the pageCond
	 */
	public PageCond getPageCond() {
		return pageCond;
	}

	/**
	 * @param pageCond the pageCond to set
	 */
	public void setPageCond(PageCond pageCond) {
		this.pageCond = pageCond;
	}

	/**
	 * @return the pageSize
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * @param pageSize the pageSize to set
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	/**
	 * @return the tempPointDataStatisPage
	 */
	public ResultWithPageCond<ITempPointDataStatistics> getTempPointDataStatisPage() {
		return tempPointDataStatisPage;
	}

	/**
	 * @param tempPointDataStatisPage the tempPointDataStatisPage to set
	 */
	public void setTempPointDataStatisPage(
			ResultWithPageCond<ITempPointDataStatistics> tempPointDataStatisPage) {
		this.tempPointDataStatisPage = tempPointDataStatisPage;
	}

	/**
	 * @return the tpdStatisExtendMap
	 */
	public Map<String, TempPointDataStatisExtendQuery> getTpdStatisExtendMap() {
		return tpdStatisExtendMap;
	}

	/**
	 * @param tpdStatisExtendMap the tpdStatisExtendMap to set
	 */
	public void setTpdStatisExtendMap(
			Map<String, TempPointDataStatisExtendQuery> tpdStatisExtendMap) {
		this.tpdStatisExtendMap = tpdStatisExtendMap;
	}

	/**
	 * @return the tempPointDataStatis
	 */
	public ITempPointDataStatistics getTempPointDataStatis() {
		return tempPointDataStatis;
	}

	/**
	 * @param tempPointDataStatis the tempPointDataStatis to set
	 */
	public void setTempPointDataStatis(ITempPointDataStatistics tempPointDataStatis) {
		this.tempPointDataStatis = tempPointDataStatis;
	}

	/**
	 * @return the tempPointDataList
	 */
	public List<ITemperaturePointDataGather> getTempPointDataList() {
		return tempPointDataList;
	}

	/**
	 * @param tempPointDataList the tempPointDataList to set
	 */
	public void setTempPointDataList(
			List<ITemperaturePointDataGather> tempPointDataList) {
		this.tempPointDataList = tempPointDataList;
	}

	/**
	 * @return the referHouseNoMap
	 */
	public Map<String, String> getReferHouseNoMap() {
		return referHouseNoMap;
	}

	/**
	 * @param referHouseNoMap the referHouseNoMap to set
	 */
	public void setReferHouseNoMap(Map<String, String> referHouseNoMap) {
		this.referHouseNoMap = referHouseNoMap;
	}

	/**
	 * @return the tpDataStatisUID
	 */
	public String getTpDataStatisUID() {
		return tpDataStatisUID;
	}

	/**
	 * @param tpDataStatisUID the tpDataStatisUID to set
	 */
	public void setTpDataStatisUID(String tpDataStatisUID) {
		this.tpDataStatisUID = tpDataStatisUID;
	}

	/**
	 * @return the concentrator
	 */
	public IConcentrator getConcentrator() {
		return concentrator;
	}

	/**
	 * @param concentrator the concentrator to set
	 */
	public void setConcentrator(IConcentrator concentrator) {
		this.concentrator = concentrator;
	}

	/**
	 * @return the pageCityMap
	 */
	public Map<String, String> getPageCityMap() {
		return pageCityMap;
	}

	/**
	 * @param pageCityMap the pageCityMap to set
	 */
	public void setPageCityMap(Map<String, String> pageCityMap) {
		this.pageCityMap = pageCityMap;
	}

	/**
	 * @return the project
	 */
	public IProject getProject() {
		return project;
	}

	/**
	 * @param project the project to set
	 */
	public void setProject(IProject project) {
		this.project = project;
	}

	/**
	 * @return the community
	 */
	public ICommunity getCommunity() {
		return community;
	}

	/**
	 * @param community the community to set
	 */
	public void setCommunity(ICommunity community) {
		this.community = community;
	}

	/**
	 * @return the building
	 */
	public IBuilding getBuilding() {
		return building;
	}

	/**
	 * @param building the building to set
	 */
	public void setBuilding(IBuilding building) {
		this.building = building;
	}

	/**
	 * @return the batch2BaseinfoMatch
	 */
	public String getBatch2BaseinfoMatch() {
		return batch2BaseinfoMatch;
	}

	/**
	 * @param batch2BaseinfoMatch the batch2BaseinfoMatch to set
	 */
	public void setBatch2BaseinfoMatch(String batch2BaseinfoMatch) {
		this.batch2BaseinfoMatch = batch2BaseinfoMatch;
	}
	
}
